<?php

// no direct access
defined('_JEXEC') or die('Restricted access');

function BakerysBuildRoute( & $query) {

    $segments = array ();
    
    $app = JFactory::getApplication();
	$menu = $app->getMenu();
	
	if (!empty($query['Itemid'])) {
		$menuItem = $menu->getItem($query['Itemid']);
		$menuGiven = true;
	}else 
	{
		$menuItem = $menu->getActive();
		$menuGiven = false;
	}	
    
    if ( isset ($query['view'])) {
        $view = $query['view'];
    }else
    { 
    	if (!$menuGiven)
    	{
    		$query['view'] = "bakerys";
    		$view = "bakerys";
    	}	
    }
    
	$session = JFactory::getSession(); 
			
    if ( isset ($query['location'])) {
		list($location_id,$location_alias) = explode(":", $query['location']);
        $segments[] = $location_alias;
        $arr_locationid = $session->get("bakery_location_route");
        $session->clear("bakery_location_route");
        if(empty($arr_locationid) && !is_array($arr_locationid))
        {
        	$arr_locationid = array();
        }
        $key = JApplication::stringURLSafe($location_alias);
        $arr_locationid["location_$key"] = $location_id;
        $session->set("bakery_location_route",$arr_locationid);
       	unset ($query['location']);
    }
    
    switch($view)
    {
    	case "bakerys":
    		unset($query["view"]);
    		
    		$url = 'index.php?option=com_bakerys&view=bakerys';
    		$items = $menu->getItems('link', $url,true);
		    $query['Itemid'] =  $items->id; 
			
    		if ( isset ($query['catid'])) {
				list($cat_id,$cat_alias) = explode(":", $query['catid']);
	        	$segments[] = $cat_alias;
	        	$arr_catid = $session->get("bakery_catid_route");
	        	$session->clear("bakery_catid_route");
	        	if(empty($arr_catid) && !is_array($arr_catid))
	        	{
	        		$arr_catid = array();
	        	}
	        	$key = JApplication::stringURLSafe($cat_alias);
	        	$arr_catid["catid_$key"] = $cat_id;
	        	$session->set("bakery_catid_route",$arr_catid);
	       		unset ($query['catid']);
	    	}

    		break;
    	case "bakery":
    		unset($query["view"]);
    		$url = 'index.php?option=com_bakerys&view=bakery';
    		$items = $menu->getItems('link', $url,true);
		    //if (!isset($query['Itemid']))   
			//{
			    $query['Itemid'] =  $items->id; 
			//}   
			
    		if ( isset ($query['catid'])) {
				list($cat_id,$cat_alias) = explode(":", $query['catid']);
	        	$segments[] = $cat_alias;
	        	$arr_catid = $session->get("bakery_catid_route");
	        	$session->clear("bakery_catid_route");
	        	if(empty($arr_catid) && !is_array($arr_catid))
	        	{
	        		$arr_catid = array();
	        	}
	        	$key = JApplication::stringURLSafe($cat_alias);
	        	$arr_catid["catid_$key"] = $cat_id;
	        	$session->set("bakery_catid_route",$arr_catid);
	       		unset ($query['catid']);
	    	}
		    
    		if ( isset ($query['id'])) {
		        list($item_id,$item_alias) = explode(":", $query['id']);
	        	$segments[] = $item_alias;
	        	$arr_item = $session->get("bakery_item_route");
	        	$session->clear("bakery_item_route");
	        	if(empty($arr_item) && !is_array($arr_item))
	        	{
	        		$arr_item = array();
	        	}
	        	$key = JApplication::stringURLSafe($item_alias);
	        	$arr_item["item_$key"] = $item_id;
	        	$session->set("bakery_item_route",$arr_item);
		        unset ($query['id']);
		    }
    		break;	
    	case "cart":
    		unset($query["view"]);
    		$url = 'index.php?option=com_bakerys&view=cart';
    		$items = $menu->getItems('link', $url,true);
    		
		   	$query['Itemid'] =  $items->id;   
    		break;	
    	case "regisuser":
    		unset($query["view"]);
    		$url = 'index.php?option=com_bakerys&view=regisuser';
    		$items = $menu->getItems('link', $url,true);
    		
		   	$query['Itemid'] =  $items->id;   
    		break;
    	case "billaddress":
    		
    		$url = 'index.php?option=com_bakerys&view=billaddress';
    		$items = $menu->getItems('link', $url,true);
    		
		   	$query['Itemid'] =  $items->id;  
    		break;		
    }
    
    return $segments;
}

function BakerysParseRoute($segments) {
    $vars = array ();
    
	$app	= JFactory::getApplication();
	$menu	= $app->getMenu();
	$item	= $menu->getActive();
	if (!isset($item)) 
	{
		$menuGiven = false;
    	//$vars['view'] = $segments[0];
    	//$view = $segments[0];
	}else 
	{
		$menuGiven = true;
		$vars['view'] = $item->query["view"];
    	$view = $item->query["view"];
    	//echo $view;exit();
	}
	
    $session = JFactory::getSession(); 
    $arr_locationid = $session->get("bakery_location_route");
    $location_alias = JApplication::stringURLSafe($segments[0]);
    $vars["location"] = $arr_locationid["location_$location_alias"];
    $vars["location_alias"] = $location_alias;
    
    switch($view)
    {
    	case "bakerys":
    		if(count($segments) > 1)
    		{
    			$arr_catid = $session->get("bakery_catid_route");
			    $catid_alias = JApplication::stringURLSafe($segments[1]);
			    $vars["catid"] = $arr_catid["catid_$catid_alias"];
			    $vars["cat_alias"] = $catid_alias;
    		}
    		break;
    	case "bakery":
    		$showcate = true;
    		if(count($segments) <= 2)
    		{
    			$item_alias = $segments[1];
    			$showcate = false;
    		}else $item_alias = $segments[2];
    	
    		if($showcate)
    		{
    			$arr_catid = $session->get("bakery_catid_route");
			    $catid_alias = JApplication::stringURLSafe($segments[1]);
			    $vars["catid"] = $arr_catid["catid_$catid_alias"];
			    $vars["cat_alias"] = $catid_alias;
    		}	
    		
    		$arr_item = $session->get("bakery_item_route");
		    $item_alias = JApplication::stringURLSafe($item_alias);
		    $vars["id"] = $arr_item["item_$item_alias"];
		    $vars["item_alias"] = $item_alias;
    		break;
    }
    return $vars;
}
